package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.entity.Meeting;
import com.example.demo.qo.MeetingQo;
import com.example.demo.service.IMeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2017/8/22.
 */
@Controller
@RequestMapping("/meeting")
public class MeetingController {

    @Autowired
    private IMeetingService meetingService;
    //127.0.0.1/meeting/list?current=1&size=10
    @RequestMapping("/list")
    public ModelAndView meetingList(MeetingQo qo){
        Page<Meeting> page = new Page<>(qo.getCurrent(),qo.getSize());
        EntityWrapper<Meeting> wrapper  = new EntityWrapper<Meeting>();
        wrapper.orderBy("id",false);
        if(qo.getId() != null){
            wrapper.eq("id",qo.getId());
        }
        if(qo.getPeople() != null){
            wrapper.like("people",qo.getPeople());
        }
        meetingService.selectPage(page,wrapper);
        return new ModelAndView("/meeting/list").addObject("meetings",page);
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView addMeeting(Meeting meeting){
        return new ModelAndView("/meeting/add");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ModelAndView addMeetingPost(Meeting meeting){
        meetingService.insert(meeting);
        return new ModelAndView("/meeting/add").addObject("suc",1).addObject("error_info","");
    }

    @RequestMapping("/del/{id}")
    public ModelAndView delMeeting(@PathVariable("id") int id){
         meetingService.deleteById(id);
//        Meeting meeting = new Meeting();
//        meeting.setStatus(0);
//        meeting.setId(id);
//        meetingService.updateById(meeting);

        return new ModelAndView("redirect:/meeting/list");
    }

    @RequestMapping("/show/{id}")
    public ModelAndView showMeeting(@PathVariable("id")  int  id){
        // id
        Meeting meeting = meetingService.selectById(id);
        return new ModelAndView("/meeting/update").addObject("meeting",meeting).addObject("suc","").addObject("error_info","");
    }


    @RequestMapping("/update")
    public ModelAndView updateMeeting(Meeting  meeting){
        // id
        meetingService.updateById(meeting);
        return new ModelAndView("/meeting/update").addObject("suc",1).addObject("error_info","");
    }

}
